<?php

namespace App\Admin\Controllers\Order;


use App\Models\Sys\Exchange;
use App\Models\Sys\Express;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Request;

class DeliverController extends Controller
{
    use ModelForm;
    protected $header = '批量发货';

    public $express = [
        '1' => [
            'express_id'   => 1,
            'express_name' => '圆通快递',
            'express_code' => 'YT'
            ],
        '2' => [
            'express_id'   => 2,
            'express_name' => '京东快递',
            'express_code' => 'JD'
            ],
        ];

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {

        $id = Request::get('id');
        if ($id){
            $data = Exchange::where('is_delivery','2')
                ->where('id',$id)
                ->with('address')
                ->orderBy('id','desc')->get()->toArray();
        } else {
            $data = Exchange::where('is_delivery','2')
                ->with('address')
                ->orderBy('id','desc')->get()->toArray();
        }

        $express = $this->express;
        return Admin::content(function (Content $content) use ($data,$express) {

            $content->header($this->header);
            $content->description('description');
            $content->body(view('admin.order.deliver',compact('data','express')));
        });
    }

    public function batchDeliver()
    {
        $express_data = Request::get('express');
        if ($express_data) {

            $exchange_ids = [];
            foreach ($express_data as $key=>$val){
                if ($val['express_code'] && $val['express_sn']){
                    $exchange_ids[$key] = $key;
                } else {
                    unset($express_data[$key]);
                }
            }

            //查询订单是否已经有发货记录
            /*$express_list = Express::whereIn('exchange_id', $exchange_ids)->select(['id','exchange_id'])->get()->toArray();
            foreach ($express_list as $key=>$val){
                unset($exchange_ids[$val['exchange_id']]);
            }*/

            $exchange_data = Exchange::whereIn('id', $exchange_ids)
                ->where('is_delivery',2)
                ->with('address')
                ->get()->toArray();

            $insert_data = [];
            foreach ($exchange_data as $key=>$val){
                $express = $express_data[$val['id']];

                $d['express_name'] = $this->express[$express['express_code']]['express_name'];
                $d['express_sn'] = $express['express_sn'];
                $d['exchange_id'] = $val['id'];
                $d['create_at'] = time();
                $d['username'] = $val['address']['linkman'];
                $d['user_mobile'] = $val['address']['mobile'];

                $insert_data[] = $d;
            }

            DB::beginTransaction();
            try {
                $express = new Express();
                $flag1 = $express->insert($insert_data);
                $exchange = new Exchange();
                $flag2 = $exchange->whereIn('id', $exchange_ids)
                    ->update(['is_delivery' => 1]);
                if($flag1 && $flag2){
                    DB::commit();
                    $status = true;
                    $message = '发货成功';
                } else {
                    DB::rollBack();
                    $status = false;
                    $message = '发货失败';
                }
            } catch (\Exception $e) {
                DB::rollBack();
                $status = false;
                $message = '发货失败';
            }


            /*$express = new Express();
            $flag = $express->insert($insert_data);

            if ($flag){
                $exchange = new Exchange();
                $exchange->whereIn('id', $exchange_ids)
                    ->update(['is_delivery' => 1]);

                $status = true;
                $message = '发货成功';
            } else {
                $status = false;
                $message = '发货失败';
            }*/

        } else {
            $status = false;
            $message = '发货失败';
        }

        return response()->json([
            'status'  => $status,
            'message' => $message,
        ]);
    }


}
